package com.zr.subclass.service.impl;

import com.zr.share.config.ErrorMassage;
import com.zr.subclass.mapper.SubClassMapper;
import com.zr.subclass.model.pojo.SubClass;
import com.zr.subclass.model.pojo.SubClassExample;
import com.zr.subclass.model.pojo.SubClassUpdateVo;
import com.zr.subclass.model.pojo.SubPlanClassAddVo;
import com.zr.subclass.service.SubClassService;
import com.zr.utils.page.PageParameterUtil;
import com.zr.utils.page.PageUtil;
import com.zr.utils.result.ResultBuilderUtil;
import com.zr.utils.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @ClassName PlanClassServiceImpl
 * @Description  计划大类实现
 * @Author 马家奎
 * @Date 2019/11/7 20:15
 */
@Service
public class SubClassServiceImpl implements SubClassService {
    @Autowired
    private SubClassMapper subClassMapper;
    @Autowired
    private SubClassExample subClassExample;
    @Autowired
    private PageUtil<SubClass> page;
    @Autowired
    private ResultUtil resultUtil;
    @Autowired
    private SubClass planClass;
    //分页＋多条件查询
    @Override
    public PageUtil<SubClass> queryPage(PageParameterUtil pageParameterUtil) {

        page.setPageIndex(pageParameterUtil.getPageIndex());
        page.setTotalNumber(subClassMapper.getCount(pageParameterUtil));
        page.setPageSize(pageParameterUtil.getPageSize());
        page.setDataList(subClassMapper.getCurrentPageData(pageParameterUtil));

        return page;
    }
    //根据id查询
    @Override
    public SubClass queryById(Integer id) {

        return subClassMapper.selectByPrimaryKey(id);
    }
    @Transactional
    @Override
    public ResultUtil addPlanClass(SubPlanClassAddVo subPlanClassAddVo) {
        //验证名称和编码不能重复
        ResultUtil validateCode = validateCode(subPlanClassAddVo.getCode());
        if (!validateCode.getSuccess()) {
            return validateCode;
        }
        ResultUtil validateName = validateName(subPlanClassAddVo.getName());
        if (!validateName.getSuccess()) {
            return validateName;
        }
        //从session中获取创建人信息和时间及修改人信息和时间

        BeanUtils.copyProperties(subPlanClassAddVo,planClass);
        Date nowTime = new Date();
        planClass.setCreatename("马家奎");
        planClass.setCreatetime(nowTime);
        planClass.setUpdatename("马家奎");
        planClass.setUpdatetime(nowTime);

        //添加
        subClassMapper.insert(planClass);
        return ResultBuilderUtil.success(null);
    }
    //添加

    //验证编号不能重复
    private ResultUtil validateCode(String  code) {
        int countPlanClassCode = subClassMapper.querySubClassCode(code);
        if (countPlanClassCode>0){
            return ResultBuilderUtil.error(ErrorMassage.CODE_NOT_REPETITION);
        }else{

            return ResultBuilderUtil.success(null);
        }

    }
    //验证名称不能重复
    public ResultUtil validateName(String name) {
        int countPlanClassName = subClassMapper.querySubClassName(name);
        if (countPlanClassName>0){
            return ResultBuilderUtil.error(ErrorMassage.NAME_NOT_REPETITION);
        }else {

            return ResultBuilderUtil.success(null);
        }
    }

    //修改
    @Transactional
    @Override
    public ResultUtil updatePlanClass(SubClassUpdateVo subClassUpdateVo) {
        //判断该计划大类是否存在
        SubClass planClass = subClassMapper.selectByPrimaryKey(subClassUpdateVo.getId());
        if (planClass == null) {
            return ResultBuilderUtil.error(ErrorMassage.UPDATE_NOT_EXIST);
        }

        //判断该计划大类状态是否禁用
        if (planClass.getStatus()) {
            return ResultBuilderUtil.error(ErrorMassage.UPDATE_STATUS_MUST_CLOSE);
        }
        //判断计划大类名称是否重复
        ResultUtil validateName = validateName(subClassUpdateVo.getName());
        if (!validateName.getSuccess()) {
            return validateName;
        }
        //从session中获取创建人信息和时间及修改人信息和时间
        BeanUtils.copyProperties(subClassUpdateVo,planClass);
        Date nowTime = new Date();
        planClass.setUpdatename("马家奎");
        planClass.setUpdatetime(nowTime);
        //修改
        subClassMapper.updatePlanClass(subClassUpdateVo);
        return ResultBuilderUtil.success(null);

    }
    //修改状态
    @Transactional
    @Override
    public ResultUtil planClassStatus(SubClass planClass) {
        subClassMapper.subClassStatus(planClass);

        return ResultBuilderUtil.success(null);
    }
}
